import router from "@/router"
import storage from "./storage"
import store from "@/store/index"
import nprogress from "nprogress"
import 'nprogress/nprogress.css'
//判断本地是否有token,如果有,那么就放行

router.beforeEach((to, from, next) => {
    nprogress.start()
    let token = storage.get('qf-token')
    //to 目标路由对象
    //from 源路由对象
    //next是个函数,调用之后,可以理解为放行
    // console.log(to)
    if (token) {
        // //已经登入,直接放行
        // next()
        if(!store.state.permission.userMenu.length){
            store.dispatch('permission/FETCH_MENU_LIST').then(
                ()=>{
                    next({path:to.path,replace:true,query:to.query})
                }
            )
        }else{
            next()
        }
    } else {
        //没有token
        //1.访问的是登入页
        if (to.path === '/login') {
            next()
        } else {
            //2.访问的不是登入页 强行调回登入页
            next({ path: "/login" })
        }

    }
})

// 全局后置钩子函数,进入路由后调用
router.afterEach((to,from)=>{
    nprogress.done()
    // console.log(to,'====',from)
    //有个matched属性,可以获取到路由
    let matched=to.matched.filter((item,index)=>index>0)
    store.commit('permission/SET_CRUMB',matched)
})



export default router